using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using Microsoft.Win32;
using System.Drawing.Printing;
namespace BarcodePrint {
	static class Program {
		static string fileName=null;
		public static float Narrow;
		public static float Wide;
		public static int Margin;
		public static int Height;
		public static string Font;
		public static int currentHeight=0;
		public static int currentLineIndex=0;


		static Program() {
			using(RegistryKey key=Registry.CurrentUser.OpenSubKey(@"Software\BarcodePrint"))
			{
				Wide=float.Parse(key.GetValue("Wide") as string);
				Narrow=float.Parse(key.GetValue("Narrow") as string);
				Margin=int.Parse(key.GetValue("Margin") as string);
				Height=int.Parse(key.GetValue("Height") as string);
				Font=key.GetValue("Font") as string;
				key.Close();
			}
		}


		[STAThread]
		static void Main(params string[] args) {
			if(args.Length==1)
				if(System.IO.File.Exists(args[0]))
					fileName=args[0];
			if(fileName==null)
				using(OpenFileDialog od=new OpenFileDialog())
				{
					od.Filter="Barcode files|*.bar";
					if(od.ShowDialog()==DialogResult.OK)
						fileName=od.FileName;
				}

			if(fileName!=null)
			{
				//Renderer.fileName=fileName;
				Stopwatch sw=Stopwatch.StartNew();
				using(PrintPreviewDialog pdlg=new PrintPreviewDialog())
				using(pdlg.Document=new System.Drawing.Printing.PrintDocument())
				{

					pdlg.ShowInTaskbar=true;
					pdlg.Icon=new Icon(typeof(Program).Assembly.GetManifestResourceStream("BarcodePrint.print.ico"));
					//pdlg.TopMost=true;
					pdlg.MouseWheel+=new MouseEventHandler(pdlg_MouseWheel);
					pdlg.Document.PrintPage+=Document_PrintPage;
					pdlg.PrintPreviewControl.Zoom=0.75;//1.56;
					pdlg.PrintPreviewControl.Rows=3;
					pdlg.WindowState=FormWindowState.Maximized;
					pdlg.Text="Generated in "+sw.Elapsed.TotalMilliseconds.ToString()+" ms";

					Application.ThreadException+=Application_ThreadException;
					Application.Run(pdlg);
				}
			}

		}

		static void pdlg_MouseWheel(object sender,MouseEventArgs e) {
			PrintPreviewDialog p=sender as PrintPreviewDialog;
			double zoom=p.PrintPreviewControl.Zoom+e.Delta/1200d;
			if(zoom>0)
			{
				p.PrintPreviewControl.Zoom=zoom;
				p.Text=zoom.ToString();
			}
		}

		static void Application_ThreadException(object sender,System.Threading.ThreadExceptionEventArgs e) {
			MessageBox.Show("The file is not valid!"+Environment.NewLine+e.Exception.Message);
			Application.Exit();
		}

		static void Document_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e) {
			Renderer rnd=new Renderer(fileName);

			if(!(sender as PrintDocument).PrintController.IsPreview&&currentLineIndex==rnd.lines.Length)
			{
				currentLineIndex=0;
				currentHeight=0;
			}
			Size sz=rnd.RenderTextLine(e.Graphics,fileName,currentHeight);
			currentHeight+=sz.Height;

			for(int i=currentLineIndex;i<rnd.lines.Length;i++)
			{

				string str=rnd.lines[i];
				if(str.Trim()=="")
				{
					currentHeight+=Margin;
					continue;
				}

				else if(rnd.IsCommentLine(str.Trim()))
					sz=rnd.RenderTextLine(e.Graphics,str.Trim(),currentHeight);
				else
					sz=rnd.RenderBarLine(e.Graphics,str,currentHeight);

				currentHeight+=sz.Height;


				//e.Graphics.DrawString(rnd.lines[i],new Font("tahoma",20),Brushes.Black,100,currentHeight);
				currentHeight+=Margin;
				currentLineIndex++;

				if(currentHeight>1100)
				{
					currentHeight=0;
					e.HasMorePages=true;
					break;
				}
			}


			//if(first)
			//{
			//  e.Graphics.DrawString("abc",new Font("tahoma",50),Brushes.Black,100,100);
			//  e.HasMorePages=true;
			//  first=false;
			//}
			//else
			//{
			//  e.Graphics.DrawString("abc",new Font("tahoma",50),Brushes.Black,100,150);
			//  //e.HasMorePages=false;

			//}
			//Renderer rnd=new Renderer(fileName);
			//e.Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
			//rnd.Render(e);
		}
	}
}